package com.qiyi.player.core.video.provider.actors.providertile
{
   import com.qiyi.player.core.video.provider.actors.dispatcher.IDispatcher;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.video.def.ProviderTileTypeEnum;
   import com.qiyi.player.core.video.events.DispatcherEvent;
   import com.qiyi.player.core.video.events.ProviderEvent;
   import flash.events.Event;
   import com.qiyi.player.core.Config;
   import com.qiyi.player.core.video.file.actors.datatile.IFlvBlock;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.video.provider.actors.dispatcher.Dispatcher;
   
   public class InternetProvider extends HttpProvider
   {
      
      public function InternetProvider(param1:IFlvBlock, param2:ICorePlayer) {
         super(param1,param2,Config.STREAM_TIMEOUT);
         this._dispatcher = new Dispatcher(_holder);
         this._dispatcher.addEventListener(DispatcherEvent.Evt_Success,this.onDispatcherSuccess);
         this._dispatcher.addEventListener(DispatcherEvent.Evt_Failed,this.onDispatcherFailed);
         _log.info("InternetProvider has been created!");
      }
      
      private var _dispatcher:IDispatcher;
      
      private var _startPos:int;
      
      override public function get type() : EnumItem {
         return ProviderTileTypeEnum.INTERNET;
      }
      
      override public function load(param1:int) : Boolean {
         _holder.runtimeData.retryCount = 0;
         this.reload(param1);
         return super.load(param1);
      }
      
      override public function stopAndReload() : void {
         this.retry();
      }
      
      override public function destroy() : void {
         if(this._dispatcher)
         {
            this._dispatcher.stop();
            this._dispatcher.removeEventListener(DispatcherEvent.Evt_Success,this.onDispatcherSuccess);
            this._dispatcher.removeEventListener(DispatcherEvent.Evt_Failed,this.onDispatcherFailed);
            this._dispatcher = null;
         }
         super.destroy();
      }
      
      private function reload(param1:int = -1) : void {
         _holder.runtimeData.currentSpeed = 0;
         _log.info(_currentRetry == 0?"loading....":"reloading....");
         this._startPos = 0;
         if(param1 == -1)
         {
            if(_block.size > 0)
            {
               this._startPos = _block.endPosition;
            }
            else if((_block.startKeyframe) && !(_block.startKeyframe.index == 0))
            {
               this._startPos = _block.startKeyframe.position;
            }
            
         }
         else
         {
            this._startPos = param1;
         }
         if(_holder.runtimeData.cacheServerIP == "" || _holder.runtimeData.cacheServerIP == null)
         {
            this._dispatcher.stop();
            this._dispatcher.start(_segment,this._startPos);
         }
         else
         {
            _url = _segment.url;
            if(this._startPos > 0)
            {
               _url = _url + ("?start=" + this._startPos.toString());
            }
            startLoad();
         }
      }
      
      protected function onDispatcherSuccess(param1:DispatcherEvent) : void {
         this._dispatcher.stop();
         _holder.runtimeData.preDefinition = _holder.runtimeData.currentDefinition;
         var _loc2_:String = param1.data as String;
         _url = _loc2_;
         startLoad();
      }
      
      private function onDispatcherFailed(param1:DispatcherEvent) : void {
         _holder.runtimeData.useDataModel = true;
         dispatchEvent(new ProviderEvent(ProviderEvent.Evt_Failed));
      }
      
      override protected function onComplete(param1:Event) : void {
         _block.endAppend();
         super.onComplete(param1);
      }
      
      override protected function retry() : void {
         destroyStream();
         _holder.runtimeData.currentSpeed = 0;
         _currentRetry++;
         if(_currentRetry < Config.STREAM_MAX_RETRY)
         {
            super.retry();
            dispatchEvent(new ProviderEvent(ProviderEvent.Evt_Retry));
            _holder.runtimeData.retryCount = _currentRetry;
            this.reload();
         }
         else
         {
            if(_errno != 0)
            {
               _holder.pingBack.sendError(_errno);
               _holder.runtimeData.errorCode = _errno;
               _errno = 0;
            }
            dispatchEvent(new ProviderEvent(ProviderEvent.Evt_Failed));
         }
      }
   }
}
